<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Virtual Scroll</title>
  <style>
    .container {
      width: 300px;
      height: 300px;
      overflow-y: auto;
      border: 1px solid #ccc;
    }

    .content {
      position: relative;
    }

    .item {
      height: 50px;
      line-height: 50px;
      border-bottom: 1px solid #ccc;
      text-align: center;
    }
  </style>
</head>
<body>
<div id="app">
  <div class="container" ref="container" @scroll="handleScroll">
    <div class="content" :style="{ height: totalHeight + 'px' }">
      <div v-for="(item, index) in visibleItems" :key="index" class="item">
        {{ item }}
      </div>
    </div>
  </div>
</div>

<!-- 引入 Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
  new Vue({
    el: '#app',
    data() {
      return {
        items: [], // 存储所有数据
        visibleItems: [], // 存储当前可见的数据
        totalHeight: 0, // 整个内容的高度
        itemHeight: 50, // 单个数据项的高度
        containerHeight: 300, // 容器的高度
        startIndex: 0, // 可见区域的起始索引
        endIndex: 0 // 可见区域的结束索引
      };
    },
    mounted() {
      this.generateData();
      this.updateVisibleItems();
    },
    methods: {
      generateData() {
        for (let i = 0; i < 1000; i++) {
          this.items.push(`Item ${i}`);
        }
        this.totalHeight = this.items.length * this.itemHeight;
      },
      updateVisibleItems() {
        const container = this.$refs.container;
        const scrollTop = container.scrollTop;
        const start = Math.floor(scrollTop / this.itemHeight);
        const end = Math.min(
                start + Math.ceil(this.containerHeight / this.itemHeight),
                this.items.length
        );

        this.startIndex = start;
        this.endIndex = end;
        this.visibleItems = this.items.slice(start, end);
      },
      handleScroll() {
        this.updateVisibleItems();
      }
    }
  });
</script>
</body>
</html>
